package com.example.chessbattleplatform.web.Sys_test.controller;


import com.example.chessbattleplatform.Pojo.Result.WebResult;
import jakarta.servlet.http.HttpServletRequest;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.io.IOException;
import java.util.Arrays;
import java.util.Enumeration;
import java.util.HashMap;
import java.util.Map;

@RestController
@RequestMapping("/Test")
public class test {
    //手动创建日志
    private static final Logger log = LoggerFactory.getLogger(test.class);

    private Map<String, String> logMap;

    //获取 HTTP 请求测试
    @RequestMapping("/getHttpRequestTest")
    public WebResult<?> txt(HttpServletRequest req) throws IOException {
        log.info("test接口被调用");
        logMap = new HashMap<>();

        //实例获取HttpServletRequest中的所以信息
        System.out.println("————————请求头参数————————");
        logMap.put("请求方式", "请求头参数");
        Enumeration<String> headerNames = req.getHeaderNames();
        headerNames.asIterator().forEachRemaining(e -> {
            System.out.println(e + ":" + req.getHeader(e));
            logMap.put(e, req.getHeader(e));
        });

        System.out.println("————————请求路径参数————————");
        logMap.put("请求方式", "请求路径参数");

        String pathInfo = req.getPathInfo();
        System.out.println(pathInfo);
        logMap.put("请求路径", pathInfo);

        System.out.println("————————请求参数————————");
        logMap.put("请求方式", "请求参数");

        Map<String, String[]> trailerFields = req.getParameterMap();
        trailerFields.forEach((k, v) -> {
            System.out.println(k + ":" + Arrays.toString(v));
            logMap.put(k, Arrays.toString(v));

        });

        System.out.println("————————Body:参数————————");
        logMap.put("请求方式", "Body:参数");
        String body = req.getReader().lines().reduce("", String::concat);
        System.out.println(body);
        logMap.put("Body参数为：", body);
        logMap.put("时间戳：", String.valueOf(System.currentTimeMillis()));

        //返回结果 时间戳
        return WebResult.success(logMap);
    }


}
